package com.kingdee.eas.custom.hr.attendance.app;

import java.io.FileInputStream;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.hr.attendance.AttendanceRecordFactory;
import com.kingdee.eas.custom.hr.attendance.AttendanceRecordInfo;
import com.kingdee.eas.custom.hr.attendance.GateConfigCollection;
import com.kingdee.eas.custom.hr.attendance.GateConfigFactory;
import com.kingdee.eas.custom.hr.attendance.GateConfigInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.rec.util.DateUtil;
import com.kingdee.eas.tools.datatask.runtime.ExcelUtil;

public class AttendanceRecordImportFacadeControllerBean extends
		AbstractAttendanceRecordImportFacadeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.attendance.app.AttendanceRecordImportFacadeControllerBean");

	private final int HEADER_ROW_CNT;
	private final int PERSON_NUM_INDEX;
	private final int DOOR_NUM_INDEX;
	private final int DOOR_NAME_INDEX;
	private final int HIT_CARD_DATE_INDEX;
	private final int HIT_CARD_TIME_INDEX;
	private final int REMARK_INDEX;

	public AttendanceRecordImportFacadeControllerBean() {
		super();
		ResourceBundle resource = ResourceBundle
				.getBundle("com.kingdee.eas.custom.hr.attendance.client.AttendanceRecordImportCfg");
		HEADER_ROW_CNT = Integer.parseInt(resource.getString("header_row_cnt"));
		PERSON_NUM_INDEX = Integer.parseInt(resource
				.getString("person_num_index"));
		DOOR_NUM_INDEX = Integer.parseInt(resource.getString("door_num_index"));
		DOOR_NAME_INDEX = Integer.parseInt(resource
				.getString("door_name_index"));
		HIT_CARD_DATE_INDEX = Integer.parseInt(resource
				.getString("hit_card_date_index"));
		HIT_CARD_TIME_INDEX = Integer.parseInt(resource
				.getString("hit_card_time_index"));
		REMARK_INDEX = Integer.parseInt(resource.getString("remark_index"));
	}

	@Override
	protected void _importAttendanceRecord(Context ctx, String file)
			throws BOSException {

		HSSFSheet sheet = null;

		String personNum = null;
		String doorNum = null;
		String doorName = null;
		String hitCardDate = null;
		String hitCardTime = null;
		String remark = null;
		AttendanceRecordInfo info = null;
		CoreBaseCollection collection = new CoreBaseCollection();

		PersonInfo personInfo = null;
		try {
			Map gates = loadAccessDateConfig(ctx);
			Map persons = this.getPersonInfoList(ctx);
			POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream(file));
			HSSFWorkbook workbook = new HSSFWorkbook(pfs);
			sheet = workbook.getSheetAt(0);

			int rowCnt = sheet.getLastRowNum()+1;

			for (int i = HEADER_ROW_CNT; i < rowCnt; i++) {
				HSSFRow row = sheet.getRow(i);
				info = new AttendanceRecordInfo();
				personNum = ExcelUtil.getCellValue(row
						.getCell(PERSON_NUM_INDEX));
				doorNum = ExcelUtil.getCellValue(row.getCell(DOOR_NUM_INDEX));
				doorName = ExcelUtil.getCellValue(row.getCell(DOOR_NAME_INDEX));
				hitCardDate = ExcelUtil.getCellValue(row
						.getCell(HIT_CARD_DATE_INDEX));
				hitCardTime = ExcelUtil.getCellValue(row
						.getCell(HIT_CARD_TIME_INDEX));

				SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
				Date d = fmt.parse(hitCardTime);
				remark = ExcelUtil.getCellValue(row.getCell(REMARK_INDEX));

				personInfo = (PersonInfo) persons.get(personNum);

				if (personInfo == null) {
					continue;
				}
				GateConfigInfo gate = this.getGateConfigInfo(gates, doorName,
						personInfo);

				if (gate == null) {
					continue;
				}
				info.setPerson(personInfo);
				info.setGate(gate);
				info.setHitCardDate(DateUtil.parse(hitCardDate, "yyyy-MM-dd"));
				info.setHitCardTime(new Time(d.getTime()));
				info.setRemark(remark);

				collection.add(info);
			}

			AttendanceRecordFactory.getLocalInstance(ctx).addnew(collection);
		} catch (Exception e) {
			throw new BOSException(e);
		}
	}

	private Map loadAccessDateConfig(Context ctx) throws BOSException,
			SQLException {
		Map gates = new HashMap();
		GateConfigCollection collection = GateConfigFactory.getLocalInstance(
				ctx).getGateConfigCollection();

		for (int i = 0; i < collection.size(); i++) {
			gates.put(collection.get(i).getNumber(), collection.get(i));
		}
		return gates;
	}

	private Map getPersonInfoList(Context ctx) throws BOSException {
		Map persons = new HashMap();
		PersonCollection collection = PersonFactory.getLocalInstance(ctx)
				.getPersonCollection();

		for (int i = 0; i < collection.size(); i++) {
			persons.put(collection.get(i).getNumber(), collection.get(i));
		}
		return persons;
	}

	@Override
	protected void _importAttendance(Context ctx, List list)
			throws BOSException {

		String personNum = null;
		String doorNum = null;
		String doorName = null;
		String hitCardDate = null;
		String hitCardTime = null;
		String remark = null;
		AttendanceRecordInfo info = null;
		CoreBaseCollection collection = new CoreBaseCollection();
		PersonInfo personInfo = null;
		Map gates = null;
		Map persons = this.getPersonInfoList(ctx);
		Map row = null;
		try {
			gates = loadAccessDateConfig(ctx);
			int rowCnt = list.size();
			for (int i = 0; i < rowCnt; i++) {
				row = (Map) list.get(i);
				info = new AttendanceRecordInfo();
				personNum = (String) row.get("personNum");
				doorNum = (String) row.get("doorNum");
				doorName = (String) row.get("doorName");
				hitCardDate = (String) row.get("hitCardDate");
				hitCardTime = (String) row.get("hitCardTime");
				remark = (String) row.get("remark");

				personInfo = (PersonInfo) persons.get(personNum);

				if (personInfo == null) {
					continue;
				}

				GateConfigInfo gate = this.getGateConfigInfo(gates, doorNum,
						personInfo);
				if (gate == null) {
					continue;
				}
				info.setPerson(personInfo);
				info.setGate(gate);
				info.setHitCardDate(DateUtil.parse(hitCardDate, "yyyy-MM-dd"));
				info.setHitCardTime(new Time(DateUtil.parse(hitCardTime,
						"hh:mm:ss").getTime()));
				info.setRemark(remark);
				collection.add(info);
			}
			AttendanceRecordFactory.getLocalInstance(ctx).addnew(collection);
		} catch (Exception e) {
			throw new BOSException(e);
		}
	}

	private GateConfigInfo getGateConfigInfo(Map gates, String doorNum,
			PersonInfo person) {
		String gateNumber = null;

		Set set = gates.keySet();

		Iterator it = set.iterator();

		while (it.hasNext()) {
			gateNumber = (String) it.next();
			if (gateNumber.equals(doorNum)) {

				if (person.isSpecialPerson()) {
					return (GateConfigInfo) gates.get(gateNumber);
				} else {
					if (!((GateConfigInfo) gates.get(gateNumber))
							.isBIMUDF0001()) {
						return (GateConfigInfo) gates.get(gateNumber);
					}
				}
				break;
			}
		}
		return null;
	}
}